package gui.storageunit;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import persistence.DataPersistor;

import core.ContainerFacade;

public class AddStorageUnitControllerTest implements IAddStorageUnitView{
	private String _name;
	private Boolean _okEnabled; 
	private AddStorageUnitController _curController;

	@Before
	public void setUp() throws Exception {
		ContainerFacade.clearAllData();
		DataPersistor.instance().init("-serial");
		
		ContainerFacade.instance().addStorageUnit("shelf1");
		ContainerFacade.instance().addStorageUnit("shelf2");
		
		_name = "";
		_okEnabled = true;
		_curController = null;
	}

	@Test
	public void testEnableComponents() {
		_curController = new AddStorageUnitController(this);
		assertFalse(_okEnabled);
		
		_name = "shelf1";
		_okEnabled = true;
		_curController.enableComponents();
		assertFalse(_okEnabled);
		
		_name = "shelf2";
		_okEnabled = true;
		_curController.enableComponents();
		assertFalse(_okEnabled);
		
		_name = "";
		_okEnabled = true;
		_curController.enableComponents();
		assertFalse(_okEnabled);	
		
		_name = "shelfe";
		_okEnabled = false;
		_curController.enableComponents();
		assertTrue(_okEnabled);		
	}

	@Test
	public void testValuesChanged() {
		_curController = new AddStorageUnitController(this);
		
		assertFalse(_okEnabled);
		
		_okEnabled = false;
		setStorageUnitName("s");
		assertTrue(_okEnabled);
		
		_okEnabled = false;
		setStorageUnitName("sh");
		assertTrue(_okEnabled);
		
		_okEnabled = false;
		setStorageUnitName("she");
		assertTrue(_okEnabled);
		
		_okEnabled = false;
		setStorageUnitName("shel");
		assertTrue(_okEnabled);
		
		_okEnabled = false;
		setStorageUnitName("shelf");
		assertTrue(_okEnabled);	
		
		_okEnabled = true;
		setStorageUnitName("shelf1");
		assertFalse(_okEnabled);	
		
		_okEnabled = true;
		setStorageUnitName("shelf2");
		assertFalse(_okEnabled);
		
		_okEnabled = false;
		setStorageUnitName("shelf2b");
		assertTrue(_okEnabled);			
	}

	@Test
	public void testAddStorageUnit() {
		_curController = new AddStorageUnitController(this);
		assertFalse(_okEnabled);
		
		setStorageUnitName("garbage");
		assertTrue(_okEnabled);
		
		_curController.addStorageUnit();
		
		assertNotNull(ContainerFacade.instance().get_rootUnit().lookupContainer("garbage"));
	}

	@Override
	public void displayInformationMessage(String message) {
		// not used
		
	}

	@Override
	public void displayWarningMessage(String message) {
		// not used
		
	}

	@Override
	public void displayErrorMessage(String message) {
		fail(message);
		
	}

	@Override
	public String getStorageUnitName() {
		return _name;
	}

	@Override
	public void setStorageUnitName(String value) {
		_name = value;
		if (_curController != null)
			_curController.valuesChanged();
		
	}

	@Override
	public void enableStorageUnitName(boolean value) {
		// not used
		
	}

	@Override
	public void enableOK(boolean value) {
		_okEnabled = value;
		
	}

}
